package com.example.suhui_test.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.suhui_test.R;

/**
 * 自定义TextView
 * 知识点：
 * 1. 继承现有View - 扩展AppCompatTextView
 * 2. 自定义属性的完整使用 - TypedArray获取属性值
 * 3. Canvas高级绘制 - 圆角矩形、阴影效果
 * 4. Paint的多种用法 - 边框、阴影、填充
 * 5. 绘制顺序控制 - 背景、边框、文字的层次
 * 6. 尺寸计算和适配
 */
public class CustomTextView extends AppCompatTextView {
    
    // 默认值
    private static final int DEFAULT_BORDER_COLOR = 0xFF666666;
    private static final float DEFAULT_BORDER_WIDTH = 2f;
    private static final float DEFAULT_CORNER_RADIUS = 8f;
    private static final int DEFAULT_SHADOW_COLOR = 0x80000000;
    private static final float DEFAULT_SHADOW_RADIUS = 4f;
    private static final float DEFAULT_SHADOW_DX = 2f;
    private static final float DEFAULT_SHADOW_DY = 2f;
    
    // 自定义属性
    private int borderColor;
    private float borderWidth;
    private float cornerRadius;
    private int shadowColor;
    private float shadowRadius;
    private float shadowDx;
    private float shadowDy;
    
    // 绘制相关
    private Paint borderPaint;
    private Paint backgroundPaint;
    private RectF rectF;
    private Path clipPath;
    
    // 是否启用自定义绘制
    private boolean hasCustomBackground = false;

    public CustomTextView(Context context) {
        this(context, null);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttributes(context, attrs);
        initPaint();
        setupShadow();
    }

    /**
     * 初始化自定义属性
     */
    private void initAttributes(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        
        borderColor = typedArray.getColor(R.styleable.CustomTextView_borderColor, DEFAULT_BORDER_COLOR);
        borderWidth = typedArray.getDimension(R.styleable.CustomTextView_borderWidth, DEFAULT_BORDER_WIDTH);
        cornerRadius = typedArray.getDimension(R.styleable.CustomTextView_cornerRadius, DEFAULT_CORNER_RADIUS);
        shadowColor = typedArray.getColor(R.styleable.CustomTextView_shadowColor, DEFAULT_SHADOW_COLOR);
        shadowRadius = typedArray.getDimension(R.styleable.CustomTextView_shadowRadius, DEFAULT_SHADOW_RADIUS);
        shadowDx = typedArray.getDimension(R.styleable.CustomTextView_shadowDx, DEFAULT_SHADOW_DX);
        shadowDy = typedArray.getDimension(R.styleable.CustomTextView_shadowDy, DEFAULT_SHADOW_DY);
        
        typedArray.recycle();
        
        // 检查是否有自定义属性，决定是否启用自定义绘制
        hasCustomBackground = borderWidth > 0 || cornerRadius > 0;
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        // 边框画笔
        borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setColor(borderColor);
        borderPaint.setStrokeWidth(borderWidth);
        
        // 背景画笔
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setStyle(Paint.Style.FILL);
        backgroundPaint.setColor(0xFFFFFFFF); // 默认白色背景
        
        // 绘制区域
        rectF = new RectF();
        clipPath = new Path();
    }

    /**
     * 设置阴影效果
     */
    private void setupShadow() {
        if (shadowRadius > 0) {
            // 为TextView设置阴影
            setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
            
            // 设置padding以容纳阴影
            int shadowPadding = (int) (shadowRadius + Math.max(Math.abs(shadowDx), Math.abs(shadowDy)));
            setPadding(
                getPaddingLeft() + shadowPadding,
                getPaddingTop() + shadowPadding,
                getPaddingRight() + shadowPadding,
                getPaddingBottom() + shadowPadding
            );
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        
        if (hasCustomBackground) {
            // 计算绘制区域，考虑边框宽度
            float halfBorderWidth = borderWidth / 2;
            rectF.set(
                halfBorderWidth,
                halfBorderWidth,
                w - halfBorderWidth,
                h - halfBorderWidth
            );
            
            // 创建圆角裁剪路径
            clipPath.reset();
            clipPath.addRoundRect(rectF, cornerRadius, cornerRadius, Path.Direction.CW);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (hasCustomBackground) {
            drawCustomBackground(canvas);
        }
        
        // 绘制文字内容
        super.onDraw(canvas);
    }

    /**
     * 绘制自定义背景和边框
     */
    private void drawCustomBackground(Canvas canvas) {
        // 保存画布状态
        canvas.save();
        
        // 绘制背景
        if (cornerRadius > 0) {
            canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, backgroundPaint);
        } else {
            canvas.drawRect(rectF, backgroundPaint);
        }
        
        // 绘制边框
        if (borderWidth > 0) {
            if (cornerRadius > 0) {
                canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, borderPaint);
            } else {
                canvas.drawRect(rectF, borderPaint);
            }
        }
        
        // 如果有圆角，裁剪画布以确保文字不会超出圆角区域
        if (cornerRadius > 0) {
            canvas.clipPath(clipPath);
        }
        
        // 恢复画布状态
        canvas.restore();
    }

    /**
     * 设置边框颜色
     */
    public void setBorderColor(int color) {
        this.borderColor = color;
        borderPaint.setColor(color);
        invalidate();
    }

    /**
     * 设置边框宽度
     */
    public void setBorderWidth(float width) {
        this.borderWidth = width;
        borderPaint.setStrokeWidth(width);
        hasCustomBackground = borderWidth > 0 || cornerRadius > 0;
        invalidate();
    }

    /**
     * 设置圆角半径
     */
    public void setCornerRadius(float radius) {
        this.cornerRadius = radius;
        hasCustomBackground = borderWidth > 0 || cornerRadius > 0;
        requestLayout(); // 圆角变化可能需要重新布局
    }

    /**
     * 设置背景颜色
     */
    public void setCustomBackgroundColor(int color) {
        backgroundPaint.setColor(color);
        invalidate();
    }

    /**
     * 设置阴影
     */
    public void setCustomShadow(float radius, float dx, float dy, int color) {
        this.shadowRadius = radius;
        this.shadowDx = dx;
        this.shadowDy = dy;
        this.shadowColor = color;
        setShadowLayer(radius, dx, dy, color);
        invalidate();
    }

    /**
     * 获取边框颜色
     */
    public int getBorderColor() {
        return borderColor;
    }

    /**
     * 获取边框宽度
     */
    public float getBorderWidth() {
        return borderWidth;
    }

    /**
     * 获取圆角半径
     */
    public float getCornerRadius() {
        return cornerRadius;
    }

    /**
     * 启用/禁用自定义背景绘制
     */
    public void setCustomBackgroundEnabled(boolean enabled) {
        this.hasCustomBackground = enabled;
        invalidate();
    }
}